// pages/receive/subscription/subscription.js
import api from '../../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '', // 搜索值
    indexList: [], // 索引
    listData: [], // 列表数据
    nodata: false, //是否没有数据
    tabType: 0, //选项卡
    show: false, // 弹窗
    actions: [{
      name: '不再订阅',
      color: '#CB3838'
    }, ],
    searchHeight: '',
    topHeight: '',
    currentPage: 1, // 当前页码
    pageSize: 20, // 每页显示数据
    tips: '',
    loading: true,
    remind: '下拉加载更多',
    description:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getData() {
    let that = this;
    let parameter = {
      dept_id:wx.getStorageSync('userInfo').depId,
      co_id:wx.getStorageSync('userInfo').coId,
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize
    };
    if (that.data.tabType == 0) {
      api.cus_subs_list({
        data: parameter,
        success: function (res) {
          console.log(res)
          that.analysis(res.data)
        }
      })
    } else {
      api.wldwList({
        data: parameter,
        success: function (res) {
          console.log(res)
          that.analysis(res.data)
        }
      })
    }
  },
  // 处理数据
  analysis(data) {
    let that = this;
    if (data.code == 200) {
      if (!data.data.list || data.data.list.length == 0) {
        that.setData({
          nodata: true
        })
      } else {
        for (let i = 0; i < data.data.list.length; i++) {
          that.data.indexList.push(data.data.list[i].initials)
        }
        let newArr = [...new Set(that.data.indexList)]  // 去重
        that.data.totalPage = data.data.totalPage;
        newArr = []
        that.setData({
          indexList: newArr,
          listData: that.data.listData.concat(data.data.list),
          loading: false,
          nodata: false,
          remind: data.data.totalPage > data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
        })
      }
    } else {
      that.setData({
        nodata: true
      })
    }
  },
  // 清空数据并重新加载数据
  clearData() {
    this.setData({
      indexList: [],
      listData: [],
      currentPage:1,
      loading: true,
      tips: this.data.tabType == 0 ? '还没有订阅的单位' : '暂无数据'
    })
    this.getData()
  },
  getHeight: function () {
    //创建节点选择器
    let query = wx.createSelectorQuery();
    let that = this;
    //选择id
    query.select('#search').boundingClientRect()
    query.select('#tabs').boundingClientRect()
    query.exec(function (res) {
      //res就是 所有标签为mjltest的元素的信息 的数组
      // console.log(res);
      //取高度
      // console.log(res[0].height, res[1].height);
      that.setData({
        searchHeight: res[0].height,
        topHeight: res[0].height + res[1].height
      })
    })
  },
  // 搜索
  onSearch(event) {
    this.setData({
      searchValue: event.detail.value ? event.detail.value : event.detail,
      loading: true,
    })
    this.clearData()
  },
  // 选项卡切换
  onChange(e) {
    this.setData({
      tabType: e.currentTarget.dataset.index,
      searchValue: '',
    })
    this.clearData()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  // 收藏,取消
  toggleCollect(e) {
    let that = this;
    that.data.param = {
      // userId: wx.getStorageSync('userInfo').unitId,
      cusId: e.currentTarget.dataset.cusid,
      // deptId: wx.getStorageSync('userInfo').depId,
      // coId: wx.getStorageSync('userInfo').coId
    }
    if (that.data.tabType == 1) {
      api.customerSubscribe({
        data: that.data.param,
        success: function (res) {
          console.log(res)
          if (res.statusCode == 200) {
            wx.showToast({
              title: '订阅成功',
            })
            that.clearData()
          }
        }
      })
    } else {
      that.setData({
        description:'不再订阅“'+e.currentTarget.dataset.name+'”，您将不能查看该单位的到款信息',
        show: true
      })
    }

  },
  // 仍然订阅
  onCancle() {
    this.setData({
      show: false
    })
  },
  // 不再订阅
  onSelect(event){
    let that = this;
    if(event.detail.name == '不再订阅'){
      api.customerUnsubscribe({
        data:that.data.param,
        success:function(res){
          console.log(res)
          if (res.statusCode == 200) {
            wx.showToast({
              title: '取消订阅成功',
            })
            that.clearData()
          }
        }
      })
    }
    that.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    this.getHeight()
    this.setData({
      tips: this.data.tabType == 0 ? '还没有订阅的单位' : '暂无数据'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading: true,
    })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})